package com.mooco.dongba.common.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Aspect
@Slf4j
@Order(1)
public class SysLogAspect {
    @Pointcut("bean(sysUserServiceImpl)")
    public void doLogPointCut() {}

    @Pointcut("bean(sysUserServiceImplEx)")
    public void doEx() {}

    @Around("doLogPointCut()")
    public Object around(ProceedingJoinPoint jp) throws Throwable {
        try {
            long t1 = System.currentTimeMillis();
            log.info("start:{}", t1);
            /* 测试切面链的执行顺序 */
            System.out.println("log 01");
            System.out.println("log 02");
            System.out.println("log 03");
            System.out.println("log 04");
            System.out.println("log 05");
            System.out.println("log 06");
            Object result = jp.proceed();
            long t2 = System.currentTimeMillis();
            log.info("end:{}", t2);
            log.info("目标方法执行时长：{}", t2 - t1);
            return result;
        } catch (Throwable e) {
            log.error("after:{}", e.getMessage());
            throw e;
        }
    }

    @Around("doEx()")
    public Object aroundEx(ProceedingJoinPoint jp) throws Throwable {
        try {
            long t1 = System.currentTimeMillis();
            log.info("start:{}", t1);
            Object result = jp.proceed();
            long t2 = System.currentTimeMillis();
            log.info("end:{}", t2);
            log.info("目标方法执行时长：{}", t2 - t1);
            return result;
        } catch (Throwable e) {
            log.error("after:{}", e.getMessage());
            throw e;
        }
    }
}
